package qj.util.concurrent;

import qj.util.ThreadUtil4;
import qj.util.funct.P0;

public class RepeatingThread implements Runnable {
	private final P0 proc;
	private final int interval;

	public RepeatingThread(int interval, P0 proc) {
		this.interval = interval;
		this.proc = proc;
		
		new Thread(this).start();
	}

	public boolean toggle() {
		return running = !running;
	}
	public boolean start() {
		return running = true;
	}

	private boolean running = false;
	public void run() {
		while (true) {
			if (running) {
				ThreadUtil4.sleep(interval);
				proc.e();
			} else {
				ThreadUtil4.sleep(200);
			}
		}
	}
}
